(function($) {
    $(function(event) {
    	$('#visa_senator_service_id').change(function() {
    		onServiceChange();
    	});
    	$('#visa_senator_entry_country').change(function() {
    		onCountryChange();
    	});
    });
})(jQuery);

function onCountryChange() {
	if (!$('#visa_senator_entry_country').val()) {
		hideServiceDetails();
		$('#visa_senator_service').hide();
	}
	else {
		getServiceList();
	}
}

function onServiceChange() {
	serviceId = $('#visa_senator_service_id').val();
	if (!serviceId) {
		hideServiceDetails();
		return;
	}
	countryId = $('#visa_senator_entry_country').val();
	if (!countryId) {
		alert('Укажите страну назначения');
		return;
	}
	getServiceInfo();
}

function getServiceList() {
	countryId = $('#visa_senator_entry_country').val();
	if (!countryId) {
		return;
	}
	req = {
        'country_id' : countryId
    };
	$.get("/a_cart.getVisaSenatorServiceList", req, onServiceListCallback, "json");
    function onServiceListCallback(resp, textStatus) {
        if (!resp.error) {
        	servicesSelect = $('#visa_senator_service_id');
        	selectedId = servicesSelect.val();
        	servicesSelect.empty();
			if (resp.services.length == 0) {
				$('#visa_senator_service').hide();
				return;
			}
			if (resp.services.length > 1) {
        		servicesSelect.append('<option value="">Выберите&hellip;</option>');
        	}
        	serviceFound = false;
        	for (i=0; i < resp.services.length; i++) {
        		selected = "";
        		if (resp.services[i].id == selectedId) {
        			selected = " selected='selected' ";
        			serviceFound = true;
        		} 
        		servicesSelect.append('<option value="' + resp.services[i].id + '"' + selected + '>' + resp.services[i].name + '</option>');
        	}        	 
        	if (serviceFound) {
				getServiceInfo();
        	}
        	else {
        		hideServiceDetails();
        	}
        	$('#visa_senator_service').show();
        }
        else {
            
        }
    }
}

function getServiceInfo() {
	serviceId = $('#visa_senator_service_id').val();
	countryId = $('#visa_senator_entry_country').val();
	if (!serviceId || !countryId) {
		return;
	}
	req = {
        'service_id' : serviceId,
        'country_id' : countryId
    };
    $.get("/a_cart.getVisaSenatorServiceInfo", req, onServiceChangeCallback, "json");
    function onServiceChangeCallback(resp, textStatus) {
        if (!resp.error) {
        	$('#visa_senator_requirement').show();
        	$('#visa_senator_requirement_text').html(resp.requirements);
        	
        	pricesSelect = $('#visa_senator_price_id');
        	pricesSelect.empty();
        	if (resp.prices.length > 1) {
        		pricesSelect.append('<option value="">Выберите&hellip;</option>');
        	}
        	for (i=0; i < resp.prices.length; i++) {
        		pricesSelect.append('<option value="' + resp.prices[i].id + '">' + resp.prices[i].name + '</option>');
        	}
        	$('#visa_senator_price').show();
        }
        else {
            alert('Выбранный тип визы не поддерживается в стране назначения');
            hideServiceDetails();
        }
    }
}

function hideServiceDetails() {
	$('#visa_senator_requirement').hide();
	$('#visa_senator_price').hide();
}